$(function(){
	var tableSource = new $.jqx.dataAdapter({
		dataType: "json",
		dataFields:[
		    { name: "id", type: "int" },
		    { name: "uuid", type: "string" },
		    { name: "creator", type: "string" },
		    { name: "roles", type: "string" },
		    { name: "createtime", type: "date" },
		    { name: "account", type: "string" },
		],
		id: "id",
		url: "admin/all.do"
	});
	$("#dataTable").jqxDataTable({
		width: "100%",
        source: tableSource,
        pageable: true,
        serverProcessing: true,
        pageSize: 18,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [
            { text: "序号", width: 50, cellsRenderer: function(row){ return (row + 1).toString(); } },
            { text: "账号", dataField: "account" },
            { text: "创建时间", dataField: "createtime", width: 150,
            	cellsRenderer: function(row, column, value, rowData){
            		return format(value, "Y-M-D H:I");
            	}
            }
        ],
	});
	$("#dataTable").on();
	$("#dataTable").on("rowDoubleClick",function(info, args){
		if(args && args.flag){
			info = args;
		}
		editingIndex = info.args.index;
		editingData = info.args.row;
	 	_editerIndex = layer.open({
			id: "sellerEditer",
			title: "查看/添加管理员信息",
		    type: 1,
		    skin: 'layui-layer-rim',
		    area: ['700px', '480px'],
		    content: 
		    	info.args.type =="add"?
		    			'<div id="admin-editer" class="form" style="height: 100%;overflow: auto;">' +
		    			'<input type="hidden" id="submit-url" value="admin/add.do" />' +
		    			'<input type="hidden" name="roles" />' +
		    			'<label class="from-label update" for="account">账号：</label>' +
		    			'<input type="text" name="account" allowBlank="false" class="form-control" placeholder="账号" />' +
		    			'<label class="from-label" allowBlank="false" for="password">密码：</label>' +
		    			'<input type="text" name="password" class="form-control" placeholder="密码" />' +
		    			'<div>' +
		    			'<label style="float:left" class="from-label update" for="percodes">角色：</label>' +
		    			'<div id="perTree" style="float:left"></div>' +
		    			'</div>' +
		    			'</div>' +
		    			'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
		    			'<input type="button" class="btn btn-sm btn-success" onclick="submitEditer(this)" value="保存"/>' +
		    			'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this, true)" value="返回"/>' +
		    			'</div>'
		    			:
			    		'<div id="admin-editer" class="form" style="height: 100%;overflow: auto;">' +
		    			'<input type="hidden" id="submit-url" value="admin/update.do" />' +
		    			'<input type="hidden" name="roles" />' +
		    			'<input type="hidden" name="uuid" />' +
						'<div>' +
						'<label style="float:left" class="from-label update" for="percodes">角色：</label>' +
						'<div id="perTree" style="float:left"></div>' +
						'</div>' +
						'</div>' +
			    		'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
		    			'<input type="button" class="btn btn-sm btn-success" onclick="submitEditer(this)" value="保存"/>' +
			    		'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this, true)" value="返回"/>' +
			    		'</div>',
						success : function(e) {	
							$.ajax({
								async: false,
								type:"post",
								url: "role/treeRole.do",
								success: function(data, status) {
									var treeView = $("#perTree");
						        	var source=[];
						        	var roles=editingData['roles'];
				        			var array=[];
				        			if(roles&&roles.length>0){
				        				array=roles.split(",");
				        			}
						        	for(var i = 0; i < data.data.length; i++){
						        		if(data.data[i].puuid==null){
						        			var newdata={ label: data.data[i].name,id:data.data[i].uuid,value:data.data[i].uuid,items:[] };
						        	     	if($.inArray(data.data[i].uuid,array)>-1){
						        	     		newdata.checked=true;
						        	     	}
						        	     	source.push(newdata);
						        		}
						        	}
									treeView.jqxTree({checkboxes:true,source: source, width: 200, height: 300});
									treeView.on('checkChange', function (event) 
									{
										var items = $('#perTree').jqxTree('getCheckedItems');
										var roles=[];
										for (var i = 0; i < items.length; i++) {
											roles.push(items[i].value);
										}
										$("[name=roles]").val(roles.join(","));
									}); 
								}
							});
						}
		});
	 	setData($("#admin-editer"), editingData);
	});
});